//
//  HWContentVoiceView.m
//  JWC
//
//  Created by lanou on 16/4/11.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWContentVoiceView.h"
#import "HWDetailPictureController.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "XFVociePlayerController.h"

@interface HWContentVoiceView ()

@property (nonatomic, strong)XFVociePlayerController *voicePlayer;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UILabel *playCount;
@property (weak, nonatomic) IBOutlet UILabel *playTime;

@end

@implementation HWContentVoiceView

- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    // 给图片添加监听器
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicture)]];
    
}

+(instancetype)voiceView {
    
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    
}

-(void)showPicture {
    
    HWDetailPictureController *showPicVc = [[HWDetailPictureController alloc]init];
    showPicVc.model = self.model;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicVc animated:YES completion:nil];
}

- (void)setModel:(HWRecommendModel *)model
{
    _model = model;
    self.playCount.text = [NSString stringWithFormat:@"%ld播放", model.audio.playcount];
    NSArray *urlArray = model.audio.thumbnail;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlArray.firstObject]];
    NSInteger m = (NSInteger)model.audio.duration;
    self.playTime.text = [NSString stringWithFormat:@"%02ld:%02ld", m / 60, m % 60];
}
- (IBAction)playButton:(UIButton *)sender
{
    self.playBtn.hidden = YES;
    self.voicePlayer = [[XFVociePlayerController alloc]initWithNibName:@"XFVociePlayerController" bundle:nil];
    self.voicePlayer.url = self.model.audio.audio.firstObject;
    self.voicePlayer.totalTime = self.model.audio.duration;
    self.voicePlayer.view.width = self.imageView.width;
    self.voicePlayer.view.y = self.imageView.height - self.voicePlayer.view.height;
    [self addSubview:self.voicePlayer.view];
}

// 重置
- (void)reset
{
    [self.voicePlayer dismiss];
    [self.voicePlayer.view removeFromSuperview];
    self.voicePlayer = nil;
    self.playBtn.hidden = NO;
}

@end






















